using UnityEngine;
using System.Collections;

[System.Serializable]
public class TurnOffLightsOnTrigger : MonoBehaviour {
	
	private GameObject[] lightsToTurnOff;
	private bool triggered = false;
	private float waitedTimeDelta = 0f;
	private bool allLightsOff = false;
	private bool soundChanged = false;
	
	private bool eventTriggered = false;
	
	public bool sortLightByDistanceAtStart = false;
	public string tagOffLightsToTurnOff = "light";
	public GameObject player;
	public string playerTag = "Player";
	public bool onExit = false;
	public float delayDeltaTime = 1.1f;
	public AudioClip lightOffSound;
	
	public bool randomTriggered = true;
	public int percentChanceToTrigger = 25;
	public int riseAnxietyLevelBy = 1;
	
	public bool testEventAlwaysTrigger = false;
	
	// Use this for initialization
	void Start () {
		Debug.Log("Startup: TurnOffLightsOnTrigger.");
		lightsToTurnOff = GameObject.FindGameObjectsWithTag(tagOffLightsToTurnOff);
		// Make sure all lights are on:
		foreach (GameObject l in lightsToTurnOff) {
			// Only light objects:
			if (l.light != null) {
				l.light.enabled = true;
			}
			
			if (l.particleSystem != null) {
				l.particleSystem.Stop();
			}
		}
		
		if (sortLightByDistanceAtStart) {
			GameObject[] temp = new GameObject[lightsToTurnOff.Length];
			Vector3 playerPos = player.transform.position;
			
			System.Array.Sort(lightsToTurnOff, delegate(GameObject i, GameObject j) {
				float iDistanceToPlayer = Vector3.Distance(playerPos, i.transform.position);
				float jDistanceToPlayer = Vector3.Distance(playerPos, j.transform.position);
				
				return (int)(iDistanceToPlayer-jDistanceToPlayer);
			});
		}
		
		Debug.Log("Startup Ended: TurnOffLightsOnTrigger.");
	}
	
	// Update is called once per frame
	void Update () {
		if (triggered && eventTriggered) {
			
			if (!soundChanged) {
				foreach (GameObject l in lightsToTurnOff) {
					if (l.audio != null) {
						l.audio.Stop();
						l.audio.clip = lightOffSound;
						l.audio.loop = false;
						l.audio.Play();
					}
				}
				soundChanged = true;
			}
			Debug.Log("Waited (" + waitedTimeDelta.ToString("#.##") + "/" + delayDeltaTime.ToString("#.##") + ")");
			if (waitedTimeDelta >= delayDeltaTime && !allLightsOff && soundChanged) {
				foreach (GameObject l in lightsToTurnOff) {
					
					if (l.particleSystem != null) {
						l.particleSystem.Play();
					}
					// Play sound on light, if any
					// eg: power surge, blackout sounds, etcs
					if (l.audio != null && lightOffSound != null) {
						l.audio.PlayOneShot(lightOffSound);
					}
					
					if (l.light != null) {
						l.light.enabled = false;
					}
					if (l.audio != null) {
						// ALso turn off all AudioSources with the same tag as the lights, 
						// eg; generators, buzzing from lamps, etc
						l.audio.enabled = false;
					}
				}
				allLightsOff = true;				
				Game.game.anxietyLevel += 1;
			} else if (!allLightsOff){
				waitedTimeDelta += Time.deltaTime;
			} else if (allLightsOff) {
				GameObject.Destroy(this);
			}
		}
	}
	
	void OnTriggerEnter(Collider collision) {
		if (collision.tag == playerTag && !onExit) {
			doTrigger();
        }
	}
	
	void doTrigger() {
		triggered = true;
		if (randomTriggered) {
			//if ()
			
			eventTriggered = (Game.game.random.Next(1, 100/percentChanceToTrigger) == 1);
			Debug.Log("Event triggered (" + eventTriggered + ")");
		} else {
			eventTriggered = true;
		}
	}
	
	void OnTriggerExit(Collider collision) {
		if (collision.tag == playerTag && onExit) {
			triggered = false;
        }
	}
}
